home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
varia
/
egebook.lha
/
ege.book
/
5
/
Collection.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-06-07
|
2KB
|
96 lines
#include <iostream.h>
#include "Collection.h"
#include "Professor.h"
#include "Course.h"
#include "TeachingAssistant.h"
// body for class Collection
int Collection::GrowIncrement = 5;
void Collection::add(Example *element) {
if (filled >= size) {
Example **oldlist = list;
list = new (Example *[size + GrowIncrement]);
for (int i = 0; i < size; i++)
list[i] = oldlist[i];
size += GrowIncrement;
};
list[filled++] = element;
};
Example* Collection::search(String &key) {
for (int i = 0; i < filled; i++)
if (list[i]->identify(key))
return list[i];
return NULL;
};
Example* Collection::search(Example *key) {
for (int i = 0; i < filled; i++)
if (list[i] == key)
return list[i];
return NULL;
};
void Collection::remove(Example *element) {
int j = 0;
Example **oldlist = list;
for (int i = 0; i < filled; i++)
if (oldlist[i] != element)
list[j++] = oldlist[i];
filled = j;
};
void Collection::print() {
if (filled == 0)
cout << "collection is empty\n";
else
for (int i = 0; i < filled; i++)
list[i]->print();
};
void Collection::maintain(){
String selection, answer;
Example *sel_example;
while (TRUE) {
print();
cout << "Select an entry or enter 0 to exit: ";
cin >> selection;
if (selection == "0") break;
if ((sel_example = search(selection)) != NULL) {
sel_example->print();
cout << "maintain (m) or delete (d) ? ";
cin >> selection;
if (selection.contains("d"))
remove(sel_example);
else
sel_example->maintain();
} else {
cout << "not found, do you want to create it (y/n) ? ";
cin >> answer;
if (answer.contains("y"))
add_alike(selection);
};
};
};
Example* Collection::select() {
String selection;
print();
cout << "enter name: ";
cin >> selection;
return(search(selection));
};
void Collection::add_alike(String &name) {
if (filled == 0)
cout << "sorry, list is empty, cannot add\n";
else {
Example *element = list[0]->alike(name);
if (!search(element))
add(element);
};
};